<?php
class categoryEditAction extends sfActions
{
	public function execute($request)
	{
	   
		$this->gallery_category_id = $request->getParameter('gallery_category_id');
		if ($this->gallery_category_id) {
			$this->gallery_category = Doctrine_Core::getTable('VTNSGalleryCategory')->find($this->gallery_category_id);
            if(isset($this->gallery_category))
			     $this->gallery_category_form = new AdminGalleryCategoryForm($this->gallery_category);
		}
        else {
            $this->gallery_category_form = new AdminGalleryCategoryForm();
        }

		if ($request->hasParameter('gallery_category')) {
			$form = $request->getParameter('gallery_category');
			$this->status = $this->updateGalleryCategoryFromRequest($this->gallery_category, $form);
			if ($this->status) {
				$this->gallery_category->save();
				$this->forward('gallery', 'categoryList');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
    
	private function updateGalleryCategoryFromRequest(&$gallery_category, $form)
	{
	    if ($form['gallery_category_id'])
			$gallery_category->gallery_category_id = $form['gallery_category_id'];
		if ($form['name'])
			$gallery_category->name = $form['name'];
		if ($form['position'])
			$gallery_category->position = $form['position'];
		$gallery_category->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>